{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1e61c7e1",
   "metadata": {},
   "source": [
    "# `cannny` 边缘检测\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "041ae9db",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "\n",
    "import cv2 as cv \n",
    "def cv_show(name, img):\n",
    "    cv.namedWindow(name, cv.WINDOW_NORMAL)\n",
    "    cv.imshow(name, img)\n",
    "    cv.waitKey(0)\n",
    "    cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "647d4960",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "img = cv.imread(\"./1.png\")\n",
    "# print(img)\n",
    "img = cv.Canny(img, 100, 255)\n",
    "cv_show( \"Canny\", img)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "205c9d3e",
   "metadata": {},
   "source": [
    "# 图像轮廓\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc68f400",
   "metadata": {},
   "source": [
    "- 为了可以更准确的得到轮廓，我们使用二值图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4f3630c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "img = cv.imread(\"./1.png\")\n",
    "gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # 转为灰度图\n",
    "ret ,thresh = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9a040d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7c8761b",
   "metadata": {},
   "source": [
    "- 绘制轮廓\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2e96b9a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "draw_img = img.copy()\n",
    "res = cv.drawContours(draw_img, contours, -4, (0, 0, 255), 2)\n",
    "cv_show(\"res\", res)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f06c614",
   "metadata": {},
   "source": [
    "- 模版匹配"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7226f767",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.1622776601683795\n"
     ]
    }
   ],
   "source": [
    "img = cv.imread(\"1.png\")\n",
    "import numpy as np\n",
    "\n",
    "print(np.sqrt(10))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
